package com.cloudsigns.factorymanager2.produce.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.activity.ProduceDispatchWorkPartOrderActivity1;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceDispatchOrderDetailAdapter1 extends BaseAdapter {
    private Context context;
    private List<WorkPartBean> workPartList = new ArrayList<WorkPartBean>();
    private LayoutInflater inflater;

    public ProduceDispatchOrderDetailAdapter1(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setData(List<WorkPartBean> list) {
        this.workPartList.clear();
        if (null != list) {
            this.workPartList.addAll(list);
        }
    }

    @Override
    public int getCount() {
        return workPartList.size();
    }

    @Override
    public WorkPartBean getItem(int position) {
        return workPartList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_produce_dispatch_order_detail1, null);
            holder.tv_workpart_name = convertView.findViewById(R.id.tv_workpart_name);
            holder.tv_last_workpart = convertView.findViewById(R.id.tv_last_workpart);
            holder.lv_dispatch_information = convertView.findViewById(R.id.lv_dispatch_information);
            holder.adapter = new ProduceWorkPartDispatchInformationAdapter(context);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final WorkPartBean workPartBean = workPartList.get(position);

        holder.adapter.setData(workPartBean.getWorkPartOrderList());
        holder.lv_dispatch_information.setAdapter(holder.adapter);

        holder.tv_workpart_name.setText(workPartBean.getWorkPartName());
        if (workPartBean.isLastWorkPart()) {
            holder.tv_last_workpart.setVisibility(View.VISIBLE);
        } else {
            holder.tv_last_workpart.setVisibility(View.GONE);
        }

        holder.lv_dispatch_information.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                WorkPartBean wpBean = workPartList.get(position);
                WorkPartOrderBean workPartOrderBean = workPartBean.getWorkPartOrderList().get(i);
                Intent mIntent = new Intent(context, ProduceDispatchWorkPartOrderActivity1.class);
                mIntent.putExtra(ConstantUtil.KEY_1, wpBean);
                mIntent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
                ((Activity) context).startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView tv_workpart_name;
        TextView tv_last_workpart;
        ListViewForScrollView lv_dispatch_information;

        ProduceWorkPartDispatchInformationAdapter adapter;
    }
}
